package com.tulei.common.utils;

import cn.hutool.core.util.StrUtil;
import com.tulei.common.config.MinioConfig;
import io.minio.*;
import io.minio.errors.MinioException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Component
public class MinioUtils {


    @Resource
    private MinioClient minioClient;

    @Resource
    private MinioConfig minioConfig;


    public boolean doesBucketExist(String bucketName) throws MinioException, IOException, NoSuchAlgorithmException, InvalidKeyException {
        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
    }

    public void createBucketIfNotExists(String bucketName) throws Exception {
        if (!doesBucketExist(bucketName)) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    public String uploadFile(String bucketName, String objectName, String filePath) throws Exception {
        createBucketIfNotExists(bucketName);
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .filename(filePath)
                        .build());
        return minioConfig.getEndpoint() + "/" + bucketName + "/" + objectName;
    }

    public String upload(MultipartFile file, String bucketName) throws Exception {
        String filename = file.getOriginalFilename();
        if (StrUtil.isNullOrUndefined(filename)) {
            throw new Exception("文件名异常");
        }
        InputStream in = null;
        try {
            filename = URLDecoder.decode(filename, "utf-8");
            in = file.getInputStream();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(filename)
                    .stream(in, in.available(), -1)
                    .contentType(file.getContentType())
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return minioConfig.getEndpoint() + "/" + bucketName + "/" + filename;
    }

    public void downloadFile(String bucketName, String objectName, String localFilePath) throws Exception {
        minioClient.downloadObject(
                DownloadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .filename(localFilePath)
                        .build());
    }

    public void removeObject(String bucketName, String objectName) throws Exception {
        minioClient.removeObject(
                io.minio.RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }
}